package com.disney.dolcn.invitemail.logic;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import com.disney.dolcn.core.cache.CacheEngine;
import com.disney.dolcn.core.cache.ICacheConn;
import com.disney.dolcn.core.common.StoreBean;
import com.disney.dolcn.core.database.IDbTemplate;
import com.disney.dolcn.core.database.ITransDealor;
import com.disney.dolcn.core.database.SqlDealer;
import com.disney.dolcn.invitemail.bean.Coupon;
import com.disney.dolcn.invitemail.bean.SendMailObject;
import com.disney.dolcn.invitemail.dao.InviteUserDao;
import com.disney.dolcn.invitemail.util.ContextUtil;
import com.disney.dolcn.invitemail.util.InviteConstant;
import com.disney.dolcn.invitemail.util.TaskTime;

/**
 * @author hawang
 * 
 */
public final class InviteLogicImpl implements InviteLogic {

	private IDbTemplate dbTemplate = null;

	/**
	 * 
	 * @param dbtemplate
	 */
	public InviteLogicImpl(IDbTemplate dbtemplate) {
		this.dbTemplate = dbtemplate;
	}

	/**
	 * 
	 */
	public InviteLogicImpl() {

	}

	/**
	 * 添加推荐人
	 * 
	 * @param name
	 * @param email
	 * @param emails
	 *            推荐的邮件联系人 多个地址用;号隔开。
	 * @param req
	 * 
	 * @return 0 为成功 1为失败
	 */

	public int addInviteUser(final String name, final String email,
			final String emails, final String path) {
		System.out.println(" 进入 方法 logic addInviteUser");
		if (InviteConstant.CONTEXT.length() < 10) {
			setInviteEmailInfo(path);// 推荐邮件功能，则初始化邮件模板内容，开启发送邮件监听
			TaskTime.getInstanst().BeginTaskTime();// 第一次 有人触发
		}
		try {
			InviteUserDao dao = new InviteUserDao();
			dao.addUser(name, email, emails);
			return 0;
		} catch (Exception d) {
			d.printStackTrace();
			return 1;
		}
	}

	/**
	 * @param templetPath
	 */
	public void setInviteEmailInfo(String templetPath) {
		try {
			String context = ContextUtil.readMailTemplate(templetPath);
			InviteConstant.CONTEXT = context;
		    System.out.println("setInviteEmailInfo邮件模板内容是："+InviteConstant.CONTEXT);
		} catch (Exception d) {
		}
	}

	/**
	 * 
	 * @return
	 */
	public List getInviteEmail() {

		final StoreBean<List<HashMap>> storeBean = new StoreBean<List<HashMap>>();
		try {
			this.dbTemplate.doTransaction(new ITransDealor() {
				public void deal(final SqlDealer sqlDealer) throws SQLException {
					InviteUserDao dao = new InviteUserDao(sqlDealer);
					dao.getListUser();
					List list = dao.getListUser();
					if (list != null && list.size() > 0) {
						storeBean.setBean(list);
					}
				}
			});
			List l=storeBean.getBean();
			if(l==null)return new ArrayList();
			System.out.println("数据库提取邮件getInviteEmail==="+l.size());
			return storeBean.getBean();
		} catch (Exception d) {
			d.printStackTrace();
			return null;
		}

	}

	/**
	 * 
	 * @param username
	 * @param email
	 * @return
	 * @throws SQLException
	 */
	public int getListUser(final String username, final String email)
			throws SQLException {
		final SendMailObject storeBean = new SendMailObject();
		try {
			this.dbTemplate.doTransaction(new ITransDealor() {
				public void deal(final SqlDealer sqlDealer) throws SQLException {
					InviteUserDao dao = new InviteUserDao(sqlDealer);
					dao.getListUser();
					int size = dao.getListUser(username, email);
					storeBean.setRd(size);

				}
			});
			return storeBean.getRd();
		} catch (Exception d) {
			d.printStackTrace();
			return 0;
		}

	}

	/**
	 * 
	 * @param ids
	 */
	public void setInviteEmail(final String ids) {
		try {
			InviteUserDao dao = new InviteUserDao();

			///System.out.println("##########setInviteEmail:" + ids);
			dao.updateUser(ids);

		} catch (Exception d) {
			d.printStackTrace();
		}

	}

	/**
	 * 取随即的优惠卷对象
	 * 
	 * @param name
	 * @param email
	 * @return
	 */
	public Coupon getRandomCoupon(String name, String email)throws Exception  {
		ICacheConn cacheConn = CacheEngine.getInstance().getCacheConn(InviteConstant.COUPON_CACHE_NAME);
		int index_ = new Random().nextInt(3);// 返回一个随机坐标
		List list = null;
		Coupon c = null;
		String tag = InviteConstant.COUPON_CACHE_KEY_3;
		if (index_ == 0) {
			tag = InviteConstant.COUPON_CACHE_KEY_1;
		}
		if (index_ == 1) {
			tag = InviteConstant.COUPON_CACHE_KEY_2;
		}
		if (index_ == 2) {
			tag = InviteConstant.COUPON_CACHE_KEY_3;
		} 
		list = (List) cacheConn.get(tag);// get List from memercached 
		if (list == null) {
			initCoupon();// init Coupon
			list = (List) cacheConn.get(tag);
		}
		if (list.size() < 1)
			list = (List) cacheConn.get(InviteConstant.COUPON_CACHE_KEY_3);// /当优惠卷用完时，这里取壁纸
		c = (Coupon) list.get(0);
		if (index_ < 2) {
			list.remove(0);// /壁纸不更新
			cacheConn.replace(tag, list);// 更新缓存内的优惠卷数量 
			try {// 数据库修改此信息状态
				InviteUserDao dao = new InviteUserDao();
				dao.upadteCoupon(c.getId());// 数据库修改此信息状态
			} catch (Exception d) {
				d.printStackTrace();
			}
		}

		return c;
	}

	/**
	 * 初始化 优惠卷到缓存
	 */
	public void initCoupon() {
		final StoreBean<List<List>> storeBean = new StoreBean<List<List>>();
		try {
			this.dbTemplate.doTransaction(new ITransDealor() {
				public void deal(final SqlDealer sqlDealer) throws SQLException {
					InviteUserDao dao = new InviteUserDao(sqlDealer);
					List l = dao.getCouponList();
					if (l == null)
						l = new ArrayList();
					storeBean.setBean(l);
				}
			});

		} catch (Exception d) {
			d.printStackTrace();
		}
		ICacheConn cacheConn = CacheEngine.getInstance().getCacheConn(
				InviteConstant.COUPON_CACHE_NAME);

		System.out.println("111111111111111111111111111111111111111111111111111111111111");
		cacheConn.add(InviteConstant.COUPON_CACHE_KEY_1, (List) storeBean
				.getBean().get(0));
		cacheConn.add(InviteConstant.COUPON_CACHE_KEY_2, (List) storeBean
				.getBean().get(1));
		cacheConn.add(InviteConstant.COUPON_CACHE_KEY_3, (List) storeBean
				.getBean().get(2));
		System.out.println("#########coupon date put in member");
		// /将列表存入缓存
	}

}
